# 模板分组事件及数据绑定
import json

from PySide2 import QtWidgets
from PySide2.QtGui import QIcon
from PySide2.QtWidgets import QHBoxLayout, QPushButton, QWidget, QMessageBox

from robotController import share
from robotController.binding.conditions_pop import Create_Group_POP, Update_Group_POP, Create_Unit_POP, Update_Unit_POP
from robotController.util.TableWidgetUtil import TableWidgetUtil
from robotController.util.requestUtil import requestUtil


class event_bind:
    # 事件绑定
    def __init__(self):
        self.ui = share.SI.main.ui
        # 新建分组按钮事件绑定
        self.ui.create_group_button.clicked.connect(lambda : self.create_group_pop())
        self.ui.create_group_button.setIcon(QIcon("./static/img/新建.png"))
        # 新建单位按钮事件绑定
        self.ui.create_unit_button.clicked.connect(lambda : self.create_unit_pop())
        self.ui.create_unit_button.setIcon(QIcon("./static/img/新建.png"))

    # 新建分组窗口弹起
    def create_group_pop(self):
        Create_Group_POP().show()

    # 新建单位窗口弹起
    def create_unit_pop(self):
        Create_Unit_POP().show()

class group_data_bind:
    # 数据绑定
    def __init__(self):
        self.ui = share.SI.main.ui
        # 查询分组数据
        dataMap = requestUtil("/template/group/findAll","GET").dataMap
        for index,item in enumerate(dataMap["data"]):
            item["buttons"] = self.buttonForRow(item["id"])

        # 准备数据头
        head_list = [{'key':"编码","value":"code"},{'key':"分组名称","value":"name"},{'key':"操作","value":"buttons"}]
        # 渲染数据
        self.ui.group_tableWidget.setRowCount(0)
        TableWidgetUtil().render(self.ui.group_tableWidget,dataMap["data"],head_list)
        TableWidgetUtil().head_color_show(self.ui.group_tableWidget)

    # 修改/删除按钮组
    def buttonForRow(self,id):
        widget=QWidget()
        # 删除
        deleteBtn = QPushButton('删除')
        deleteBtn.setStyleSheet(''' QPushButton{
                                    text-align : center;
                                    background-color : transparent;
                                    height : 30px;
                                    width:50px;
                                    border-style: outset;
                                    color:red;}
                                     
                                    QPushButton:hover{
                                    	color: red;
                                    	text-decoration:underline;
                                    }''')

        # 查看
        alterBtn = QPushButton('修改')
        alterBtn.setStyleSheet(''' QPushButton{
                                    text-align : center;
                                    background-color : transparent;
                                    height : 30px;
                                    width:50px;
                                    border-style: outset;
                                    color:#192D9E;
                                    }
                                     
                                    QPushButton:hover{
                                    	color: #192D9E;
                                    	text-decoration:underline;
                                    }''')

        alterBtn.clicked.connect(lambda : self.alter_fun(str(id)))

        deleteBtn.clicked.connect(lambda : self.del_fun(str(id)))

        hLayout = QHBoxLayout()
        hLayout.addWidget(alterBtn)
        hLayout.addWidget(deleteBtn)
        hLayout.setContentsMargins(5,2,5,2)
        widget.setLayout(hLayout)
        return widget

    # 修改分组事件
    def alter_fun(self,id):
        # 发送删除请求
        dataMap = requestUtil("/template/group/findById/" + id,"GET").dataMap
        update_Group_POP = Update_Group_POP(dataMap["data"]["id"])
        # 回显数据
        update_Group_POP.group_name_LineEdit.setText(dataMap["data"]["name"])
        update_Group_POP.group_code_LineEdit.setText(dataMap["data"]["code"])
        update_Group_POP.show()

    # 删除分组事件
    def del_fun(self,id):
        A = QMessageBox.question(self.ui,'确认','是否删除该分组?',QMessageBox.Yes | QMessageBox.No)
        if A == QMessageBox.No:
            return
        # 发送删除请求
        dataMap = requestUtil("/template/group/delete/" + id,"DELETE").dataMap
        if dataMap["flag"]:
            QtWidgets.QMessageBox.about(self.ui ,"删除成功", dataMap["message"])
            # 删除成功刷新数据
            group_data_bind()
        else:
            QtWidgets.QMessageBox.about(self.ui ,"删除失败", dataMap["message"])

class unit_data_bind:
    # 数据绑定
    def __init__(self):
        self.ui = share.SI.main.ui
        # 查询分组数据
        dataMap = requestUtil("/template/unit/findAll","GET").dataMap
        for index,item in enumerate(dataMap["data"]):
            item["buttons"] = self.buttonForRow(item["id"])

        # 准备数据头
        head_list = [{'key':"编码","value":"code"},{'key':"分组名称","value":"name"},{'key':"操作","value":"buttons"}]
        # 渲染数据
        self.ui.unit_tableWidget.setRowCount(0)
        TableWidgetUtil().render(self.ui.unit_tableWidget,dataMap["data"],head_list)
        TableWidgetUtil().head_color_show(self.ui.unit_tableWidget)

    # 修改/删除按钮组
    def buttonForRow(self,id):
        widget=QWidget()
        # 删除
        deleteBtn = QPushButton('删除')
        deleteBtn.setStyleSheet(''' QPushButton{
                                    text-align : center;
                                    background-color : transparent;
                                    height : 30px;
                                    width:50px;
                                    border-style: outset;
                                    color:red;}
                                     
                                    QPushButton:hover{
                                    	color: red;
                                    	text-decoration:underline;
                                    }''')

        # 查看
        alterBtn = QPushButton('修改')
        alterBtn.setStyleSheet(''' QPushButton{
                                    text-align : center;
                                    background-color : transparent;
                                    height : 30px;
                                    width:50px;
                                    border-style: outset;
                                    color:#192D9E;
                                    }
                                     
                                    QPushButton:hover{
                                    	color: #192D9E;
                                    	text-decoration:underline;
                                    }''')

        alterBtn.clicked.connect(lambda : self.alter_fun(str(id)))

        deleteBtn.clicked.connect(lambda : self.del_fun(str(id)))

        hLayout = QHBoxLayout()
        hLayout.addWidget(alterBtn)
        hLayout.addWidget(deleteBtn)
        hLayout.setContentsMargins(5,2,5,2)
        widget.setLayout(hLayout)
        return widget

    # 修改分组事件
    def alter_fun(self,id):
        # 发送删除请求
        dataMap = requestUtil("/template/unit/findById/" + id,"GET").dataMap
        update_Unit_POP = Update_Unit_POP(dataMap["data"]["id"])
        # 回显数据
        update_Unit_POP.unit_name_LineEdit.setText(dataMap["data"]["name"])
        update_Unit_POP.unit_code_LineEdit.setText(dataMap["data"]["code"])
        update_Unit_POP.show()

    # 删除分组事件
    def del_fun(self,id):
        A = QMessageBox.question(self.ui,'确认','是否删除该单位?',QMessageBox.Yes | QMessageBox.No)
        if A == QMessageBox.No:
            return
        # 发送删除请求
        dataMap = requestUtil("/template/unit/delete/" + id,"DELETE").dataMap
        if dataMap["flag"]:
            QtWidgets.QMessageBox.about(self.ui ,"删除成功", dataMap["message"])
            # 删除成功刷新数据
            unit_data_bind()
        else:
            QtWidgets.QMessageBox.about(self.ui ,"删除失败", dataMap["message"])